﻿Public Class FormMain
    Dim objUser As New clsUser
    Dim objFlbck As New clsFLBck
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dgv.Font = New Font("UTF-8", 14)
        connectDB()
        getUserListByCJ()
        Dim date_current As String = Date.Now.ToShortDateString()
        dtRK_Begin.Text = date_current
        dtRK_End.Text = date_current
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_Warehouse_FLB.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If
    End Sub

    Private Sub getUserListByCJ()
        Dim dtUser As DataTable = objUser.getUserListByCJ(1)
        cmbOper.DisplayMember = "user_name"
        cmbOper.ValueMember = "user_id"
        cmbOper.DataSource = dtUser
    End Sub

    Private Sub setDgvData()
        Dim dt As New DataTable
        dt = objFlbck.getListALL()
        If dt.Rows.Count > 0 Then
            dt.Columns.Add("status_name")
            dt.Columns.Add("which_side_name")
            For Each row As DataRow In dt.Rows
                If row("status") = "0" Then
                    row("status_name") = "在库"
                Else
                    row("status_name") = "已出库"
                End If
                If row("which_side") = "0" Then
                    row("which_side_name") = "朝上"
                Else
                    row("which_side_name") = "朝下"
                End If
            Next
        End If

        Dim strFilter As String = ""
        strFilter &= "status=" & IIf(rdoZK.Checked = True, "'0'", "'1'") & " and "
        If txtKWH.Text <> "" Then strFilter &= "location like ('%" & txtKWH.Text & "%') and "
        If txtDLH.Text <> "" Then strFilter &= "dl_no like ('%" & txtDLH.Text & "%') and "
        If dtRK_Begin.Text <> "" Then
            If rdoZK.Checked Then
                strFilter &= "begin_date >= '" & dtRK_Begin.Text & " 00:00:00.000" & "' and "
            Else
                strFilter &= "end_date >= '" & dtRK_Begin.Text & " 00:00:00.000" & "' and "
            End If

        End If
        If dtRK_End.Text <> "" Then
            If rdoZK.Checked Then
                strFilter &= "begin_date <= '" & dtRK_End.Text & " 23:59:59.999" & "' and "
            Else
                strFilter &= "end_date <= '" & dtRK_Begin.Text & " 23:59:59.999" & "' and "
            End If
        End If

        dt.DefaultView.RowFilter = strFilter & "1=1"
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        setDgvData()
    End Sub

    Private Sub btnRk_Click(sender As Object, e As EventArgs) Handles btnRk.Click
        Dim formRk As New FormRk()
        formRk.usr = cmbOper.Text.Trim()
        formRk.ShowDialog(Me)
    End Sub

    Private Sub btnDELIVER_Click(sender As Object, e As EventArgs) Handles btnDELIVER.Click
        Dim formDeliver As New FormDeliver()
        formDeliver.usr = cmbOper.Text.Trim()
        formDeliver.ShowDialog(Me)
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

End Class
